/**
 * Eye implementation
 */

#include "Eye.h"
#include "Ray.h"

/////////////////////////////////////////////////

void kig::Eye::setCanvas(kig::Canvas* c, Float dis)
{
	ASSERT(c);
	unsigned int w = c->getWidth();
	unsigned int h = c->getHeight();
	_canvas = c;
	_distanceToCanvas = dis;
	// TODO improve eye/canvas relationship?
	if (h > w) {
		_xStep = 1.0/(Float) w;
		_yStep = (h/w)/(Float) h;
		_fX = -0.5;
		_fY = -0.5 * (h/w);
	} else {
		_yStep = 1.0/(Float) h;
		_xStep = (w/h)/(Float) w;
		_fY = -0.5;
		_fX = -0.5 * (w/h);
	}
}

/////////////////////////////////////////////////

bool kig::Eye::moreRays()
{
	return (_curX < _canvas->getWidth() && _curY < _canvas->getHeight());
}

/////////////////////////////////////////////////

kig::EyeRay kig::Eye::nextRay()
{
	ASSERT(_canvas.get());
	Vec4 dir(_fX, _fY, _distanceToCanvas, 1.0);

	kig::EyeRay r;
	r.x = _curX;
	r.y = _curY;
	r.r = kig::Ray(Vec4(0.0, 0.0, 0.0, 1.0), dir);
	
	_curX++;
	_curY++;
	_fX+=_xStep;
	_fY+=_yStep;

	return r;
}



